package com.lordjoe.collectiveintelligence;

/**
 * com.lordjoe.collectiveintelligence.FlightCost
 *
 * @author Steve Lewis
 * @date Mar 24, 2009
 */
public class ItineryCost implements ICostFunction<ItineryItem[]>
{
    public static ItineryCost[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = ItineryCost.class;
    public static final ItineryCost INSTANCE = new  ItineryCost();

    public static final double DOLLAR_COST_FACTOR = 1.0;
    public static final double MINUTE_WAIT_COST_FACTOR = 1.0;

    private ItineryCost() {}

    public double computeCost(ItineryItem[] target, Object... otherData)
    {
        double sum = 0;
        int earliestDepart = Integer.MAX_VALUE;
        int latestArrive = Integer.MAX_VALUE;

        for (int i = 0; i < target.length; i++) {
            ItineryItem item = target[i];
            earliestDepart = Math.min(earliestDepart,item.getReturn().getStartTime());
            latestArrive = Math.max(latestArrive,item.getArrival().getArriveTime());
              sum += ItineryItemCost.INSTANCE.computeCost(item,target);
        }
        if(earliestDepart > latestArrive)
            sum += 50; // Add car rental
        return sum;
    }

    public double lowestCost()
    {
        return 0;
    }
}